package com.hcss.qywechat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcss.common.constant.CommonConstants;
import com.hcss.common.constant.ConfigConstants;
import com.hcss.common.util.ListUtil;
import com.hcss.paging.service.SchemaPagingByExample;
import com.hcss.qywechat.user.dao.WQyUserComplexMapper;
import com.hcss.qywechat.user.dao.WQyUserMapper;
import com.hcss.qywechat.user.model.WQyUser;
import com.hcss.qywechat.user.model.WQyUserExample;
import com.hcss.qywechat.user.model.par.WQyUserPar;
import com.hcss.qywechat.user.model.vo.WQyUserVO;
import com.hcss.qywechat.user.service.WQyUserService;
import com.hcss.sys.role.dao.HcSysRoleuserMapper;
import com.hcss.sys.role.model.HcSysRoleuserExample;
import com.hcss.sys.user.dao.HcSysUseragentMapper;
import com.hcss.sys.user.dao.HcSysUserbelongapMapper;
import com.hcss.sys.user.model.HcSysUseragent;
import com.hcss.sys.user.model.HcSysUseragentExample;
import com.hcss.sys.user.model.HcSysUserbelongap;
import com.hcss.sys.user.model.HcSysUserbelongapExample;
import com.hcss.util.EmptyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @描述: WQyUser-服务的实现类
 * @作者: fangtao
 * @创建日期: 2019-11-05 10:35:55
 */
@Service("wQyUserService")
public class WQyUserServiceImpl implements WQyUserService {
	
	@Autowired
	private WQyUserMapper wQyUserMapper;
	@Autowired
	private WQyUserComplexMapper wQyUserComplexMapper;
	@Autowired
	private HcSysUseragentMapper hcSysUseragentMapper;
	@Autowired
	private HcSysUserbelongapMapper hcSysUserbelongapMapper;
	@Autowired
	private HcSysRoleuserMapper hcSysRoleuserMapper;
	@Autowired
	private ConfigConstants configConstants;

	@Override
	public PageInfo<WQyUser> queryPagingData(WQyUserPar par) throws Exception{
        WQyUserExample example = (WQyUserExample) SchemaPagingByExample.getBaseExample(WQyUserExample.class, par);
        PageHelper.startPage(par.getPage(), par.getPerpage());
        List<WQyUser> list =  wQyUserMapper.selectByExample(example);
        return new PageInfo<>(list);
	}
	@Override
	public PageInfo<WQyUserVO> queryPagingComplexData(WQyUserVO model) throws Exception {
		PageHelper.startPage(model.getPage(), model.getPerpage());
		model.setStatus(1L);
		List<WQyUserVO> list = wQyUserComplexMapper.queryPagingComplexData(model);
		for(WQyUserVO wQyUserVO:list){
			HcSysUseragentExample hcSysUseragentExample=new HcSysUseragentExample();
			hcSysUseragentExample.createCriteria().andUseridEqualTo(wQyUserVO.getUserid());
			List<HcSysUseragent>hcSysUseragentList=hcSysUseragentMapper.selectByExample(hcSysUseragentExample);
			List<String> agents = ListUtil.getAttrList(hcSysUseragentList,"agentcode");
			if(!EmptyUtil.isEmpty(agents)) {
				wQyUserVO.setAgentcode(StringUtils.join(agents.toArray(), CommonConstants.COMMA));
			}
		}
		return new PageInfo<>(list);
	}
	
	@Override
	public List<WQyUser> queryAllData(WQyUserPar par) throws Exception {
		WQyUserExample example = (WQyUserExample) SchemaPagingByExample.getBaseExample(WQyUserExample.class, par);
		return wQyUserMapper.selectByExample(example);
	}
	@Override
	public List<WQyUserVO> queryAllComplexData(WQyUserVO model) throws Exception {
		model.setStatus(1L);
		return wQyUserComplexMapper.queryPagingComplexData(model);
	}

	@Override
	public void insertModel(WQyUser model) {
		model.setCorpid(configConstants.getWechat().getCorpid());
		model.setStatus(1L);
		wQyUserMapper.insertSelective(model);
	}

	@Override
	public void insertUseragent(WQyUserVO model){
		HcSysUseragent hcSysUseragent=new HcSysUseragent();
		hcSysUseragent.setUserid(model.getUserid());
		hcSysUseragent.setAgentcode(model.getAgentcode());
		hcSysUseragentMapper.insertSelective(hcSysUseragent);
	}
	@Override
	public void insertUserbelongap(WQyUserVO model){
		HcSysUserbelongap hcSysUserbelongap=new HcSysUserbelongap();
		hcSysUserbelongap.setUserid(model.getUserid());
		hcSysUserbelongap.setBelongap(configConstants.getBelongAp());
		hcSysUserbelongapMapper.insertSelective(hcSysUserbelongap);
	}

	@Override
	public void updateModel(WQyUser model) {
        WQyUserExample example = new WQyUserExample();
        example.createCriteria().andIdEqualTo(model.getId());
        if(!EmptyUtil.isHaveEmpty(model.getId())) {
			wQyUserMapper.updateByExampleSelective(model, example);
		}
	}

	@Override
	public void updateComplexModel(WQyUserVO model) {
		//删除用户所属机场
		HcSysUserbelongapExample hcSysUserbelongapExample=new HcSysUserbelongapExample();
		hcSysUserbelongapExample.createCriteria().andUseridEqualTo(model.getUserid());
		hcSysUserbelongapMapper.deleteByExample(hcSysUserbelongapExample);
		//删除用户关联货代
		HcSysUseragentExample hcSysUseragentExample=new HcSysUseragentExample();
		hcSysUseragentExample.createCriteria().andUseridEqualTo(model.getUserid());
		hcSysUseragentMapper.deleteByExample(hcSysUseragentExample);
		//更新用户表
		WQyUserExample example = new WQyUserExample();
		example.createCriteria().andIdEqualTo(model.getId());
		if(!EmptyUtil.isHaveEmpty(model.getId())) {
			wQyUserMapper.updateByExampleSelective(model, example);
		}
		//重新新增关联的货代
		String[] agents =model.getAgentcode().split(CommonConstants.COMMA);
		for(String agent:agents) {
			model.setAgentcode(agent);
			insertUseragent(model);
		}
		//重新新增机场
		insertUserbelongap(model);
	}

	@Override
	public void deleteByIds(List<Long> ids) {
		for(Long id:ids){
			WQyUser wQyUser=selectByPK(id.toString());
			//删除用户所属机场
			HcSysUserbelongapExample hcSysUserbelongapExample=new HcSysUserbelongapExample();
			hcSysUserbelongapExample.createCriteria().andUseridEqualTo(wQyUser.getUserid());
			hcSysUserbelongapMapper.deleteByExample(hcSysUserbelongapExample);
			//删除用户关联货代
			HcSysUseragentExample hcSysUseragentExample=new HcSysUseragentExample();
			hcSysUseragentExample.createCriteria().andUseridEqualTo(wQyUser.getUserid());
			hcSysUseragentMapper.deleteByExample(hcSysUseragentExample);
			//删除用户角色信息
			HcSysRoleuserExample hcSysRoleuserExample=new HcSysRoleuserExample();
			hcSysRoleuserExample.createCriteria().andUseridEqualTo(wQyUser.getUserid());
			hcSysRoleuserMapper.deleteByExample(hcSysRoleuserExample);
		}
		//用户
		WQyUserExample example = new WQyUserExample();
		example.createCriteria().andIdIn(ids);
		wQyUserMapper.deleteByExample(example);
	}

	@Override
	public void deleteComplexData(WQyUserVO model){
		//用户
		WQyUserExample wQyUserExample = new WQyUserExample();
		wQyUserExample.createCriteria().andUseridEqualTo(model.getUserid());
		//用户所属机场
		HcSysUserbelongapExample hcSysUserbelongapExample=new HcSysUserbelongapExample();
		hcSysUserbelongapExample.createCriteria().andUseridEqualTo(model.getUserid());
		//用户关联货代
		HcSysUseragentExample hcSysUseragentExample=new HcSysUseragentExample();
		hcSysUseragentExample.createCriteria().andUseridEqualTo(model.getUserid());
		List<HcSysUseragent>hcSysUseragentList=hcSysUseragentMapper.selectByExample(hcSysUseragentExample);
		//若只关联一个货代则直接删除用户和所属机场
		if(hcSysUseragentList.size()<2){
			wQyUserMapper.deleteByExample(wQyUserExample);
			hcSysUserbelongapMapper.deleteByExample(hcSysUserbelongapExample);
		}
		hcSysUseragentExample.clear();
		hcSysUseragentExample.createCriteria().andUseridEqualTo(model.getUserid())
				.andAgentcodeEqualTo(model.getAgentcode());
		hcSysUseragentMapper.deleteByExample(hcSysUseragentExample);
	}
	
	@Override
	public WQyUser selectByPK(String id) {
		if (EmptyUtil.isHaveEmpty(id)) return null;
        WQyUserExample example = new WQyUserExample();
        example.createCriteria().andIdEqualTo(Long.valueOf(id)).andStatusEqualTo(1L);
		List<WQyUser> list = wQyUserMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public WQyUserVO selectDataById(Long id) throws Exception{
		if (EmptyUtil.isHaveEmpty(id)) return null;
		WQyUserVO wQyUserVO=new WQyUserVO();
		wQyUserVO.setId(id);
		wQyUserVO.setStatus(1L);
		List<WQyUserVO>list= wQyUserComplexMapper.queryPagingComplexData(wQyUserVO);
		if(!EmptyUtil.isEmpty(list)){
			wQyUserVO=list.get(0);
			HcSysUseragentExample hcSysUseragentExample=new HcSysUseragentExample();
			hcSysUseragentExample.createCriteria().andUseridEqualTo(wQyUserVO.getUserid());
			List<HcSysUseragent>hcSysUseragentList=hcSysUseragentMapper.selectByExample(hcSysUseragentExample);
			List<String> agents = ListUtil.getAttrList(hcSysUseragentList,"agentcode");
			if(!EmptyUtil.isEmpty(agents)) {
				wQyUserVO.setAgentcode(StringUtils.join(agents.toArray(), CommonConstants.COMMA));
			}
			return  wQyUserVO;
		}else{
			return null;
		}
	}

	@Override
	public Boolean checkCodeUnique(WQyUserVO model){
		boolean tempBoolean = true;
		WQyUserVO wQyUserVO=new WQyUserVO();
		wQyUserVO.setUserid(model.getUserid());
		//wQyUserVO.setStatus(1L);
		List<WQyUserVO> wQyUserVOList = wQyUserComplexMapper.queryPagingComplexData(wQyUserVO);
		if (!CollectionUtils.isEmpty(wQyUserVOList)) {// 未查询到重复code，返回true
			if (EmptyUtil.isHaveEmpty(model.getId())) {
				if(wQyUserVOList.size()>0) {
					tempBoolean = false;
				}
			} else {// 更新情况下，code不改变返回true
				for(WQyUserVO tempUsers : wQyUserVOList) {
					if(!(tempUsers.getId().intValue() == model.getId())) {
						tempBoolean =false;
					}
				}
				return tempBoolean;
			}
		}
		return tempBoolean;
	}

	@Override
	public Boolean checkUseragent(WQyUserVO model){
		boolean tempBoolean = false;
		HcSysUseragentExample hcSysUseragentExample =new HcSysUseragentExample();
		hcSysUseragentExample.createCriteria().andUseridEqualTo(model.getUserid()).andAgentcodeEqualTo(model.getAgentcode());
		List<HcSysUseragent>hcSysUseragentList=hcSysUseragentMapper.selectByExample(hcSysUseragentExample);
		tempBoolean =EmptyUtil.isEmpty( hcSysUseragentList );
		return tempBoolean;
	}

	@Override
	public Boolean checkUserbelongap(WQyUserVO model){
		boolean tempBoolean = false;
		HcSysUserbelongapExample hcSysUserbelongapExample =new HcSysUserbelongapExample();
		hcSysUserbelongapExample.createCriteria().andUseridEqualTo(model.getUserid()).andBelongapEqualTo(configConstants.getBelongAp());
		List<HcSysUserbelongap>hcSysUserbelongapList=hcSysUserbelongapMapper.selectByExample(hcSysUserbelongapExample);
		tempBoolean =EmptyUtil.isEmpty( hcSysUserbelongapList );
		return tempBoolean;
	}

	@Override
	public void resetPassword( WQyUserVO model ) {
		wQyUserComplexMapper.resetPassword(model);
	}
}
